package com.example.lkslib.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/3/28.
 */

public class ToastUtils {

    private static Handler handler = new Handler(Looper.getMainLooper());
    private static Toast toast = null;
    private static Object synObj = new Object();

    /**
     * Toast发送消息，默认Toast.LENGTH_SHORT
     *
     * @param act
     * @param msg
     */
    public static void showMessage(final Context act, final String msg) {
        showMessage(act, msg, Toast.LENGTH_SHORT);
    }

    /**
     * Toast发送消息，默认Toast.LENGTH_LONG
     *
     * @param act
     * @param msg
     */
    public static void showMessageLong(final Context act, final String msg) {
        showMessage(act, msg, Toast.LENGTH_LONG);
    }

    /**
     * Toast发送消息，默认Toast.LENGTH_SHORT
     *
     * @param act
     * @param msg
     */
    public static void showMessage(final Context act, final int msg) {
        showMessage(act, msg, Toast.LENGTH_SHORT);
    }

    /**
     * Toast发送消息，默认Toast.LENGTH_LONG
     *
     * @param act
     * @param msg
     */
    public static void showMessageLong(final Context act, final int msg) {
        showMessage(act, msg, Toast.LENGTH_LONG);
    }

    /**
     * Toast发送消息
     *
     * @param act
     * @param msg
     * @param len
     */
    public static void showMessage(final Context act, final int msg,
                                   final int len) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            new Thread(new Runnable() {
                public void run() {
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            show(msg, len, act);
                        }
                    });
                }
            }).start();
        } else {
            show(msg, len, act);
        }

    }

    private static void show(int msg, int len, Context act) {
        synchronized (synObj) {
            if (toast != null) {
                toast.cancel();
                toast.setText(msg);
                toast.setDuration(len);
            } else {
                toast = Toast.makeText(act, msg, len);
            }
            toast.show();
        }
    }

    /**
     * Toast发送消息
     *
     * @param act
     * @param msg
     * @param len
     */
    public static void showMessage(final Context act, final String msg,
                                   final int len) {
        if (Looper.myLooper() != Looper.getMainLooper()) {
            new Thread(new Runnable() {
                public void run() {
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            show(msg, len, act);
                        }
                    });
                }
            }).start();
        } else {
            show(msg, len, act);
        }
    }

    private static void show(String msg, int len, Context act) {
        synchronized (synObj) {
            if (toast != null) {
                toast.cancel();
                toast.setText(msg);
                toast.setDuration(len);
            } else {
                toast = Toast.makeText(act, msg, len);
            }
            toast.show();
        }
    }

    /**
     * 关闭当前Toast
     */
    public static void cancelCurrentToast() {
        if (toast != null) {
            toast.cancel();
        }
    }

    public static void showOneToast(Context context, String content) {
        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }
}
